//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_gitlab"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processGitlabEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		buildEvent := &events_gitlab.BuildEvent{}
		if event.EventData.MessageIs(buildEvent) {
			err = event.EventData.UnmarshalTo(buildEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s TODO", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Build {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	{
		commentEvent := &events_gitlab.CommentEvent{}
		if event.EventData.MessageIs(commentEvent) {
			err = event.EventData.UnmarshalTo(commentEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s 评论 ", event.UserDisplayName, event.ProjectName, commentEvent.User.Name)
			if commentEvent.Commit != nil && commentEvent.Commit.Url != "" {
				msg = msg + fmt.Sprintf("Commit(%s),%s ", commentEvent.Commit.Id, commentEvent.Commit.Url)
			}
			if commentEvent.Issue != nil && commentEvent.Issue.Id != 0 {
				msg = msg + fmt.Sprintf("工单 %s ", commentEvent.Issue.Title)
			}
			msg = msg + fmt.Sprintf("内容：%s", commentEvent.ObjectAttributes.Description)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Comment {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		issueEvent := &events_gitlab.IssueEvent{}
		if event.EventData.MessageIs(issueEvent) {
			err = event.EventData.UnmarshalTo(issueEvent)
			if err != nil {
				return false, err
			}
			opt := "创建"
			if issueEvent.ObjectAttributes.Action == "close" {
				opt = "关闭"
			} else if issueEvent.ObjectAttributes.Action == "reopen" {
				opt = "重新打开"
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s %s工单 %s %s。", event.UserDisplayName, event.ProjectName,
				issueEvent.User.Name, opt, issueEvent.ObjectAttributes.Title, issueEvent.ObjectAttributes.Url)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Issue {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		jobEvent := &events_gitlab.JobEvent{}
		if event.EventData.MessageIs(jobEvent) {
			err = event.EventData.UnmarshalTo(jobEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s TODO", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Job {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		mergeRequestEvent := &events_gitlab.MergeRequestEvent{}
		if event.EventData.MessageIs(mergeRequestEvent) {
			err = event.EventData.UnmarshalTo(mergeRequestEvent)
			if err != nil {
				return false, err
			}
			opt := "打开"
			if mergeRequestEvent.ObjectAttributes.Action != "open" {
				opt = "关闭"
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s %s 合并需求 %s(%s) %s 至 %s(%s) %s。", event.UserDisplayName, event.ProjectName,
				mergeRequestEvent.User.Name, opt,
				mergeRequestEvent.ObjectAttributes.Source.Name, mergeRequestEvent.ObjectAttributes.Source.Homepage, mergeRequestEvent.ObjectAttributes.SourceBranch,
				mergeRequestEvent.ObjectAttributes.Target.Name, mergeRequestEvent.ObjectAttributes.Target.Homepage, mergeRequestEvent.ObjectAttributes.TargetBranch)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.MergeRequest {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		pipelineEvent := &events_gitlab.PipelineEvent{}
		if event.EventData.MessageIs(pipelineEvent) {
			err = event.EventData.UnmarshalTo(pipelineEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s TODO", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Pipeline {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		pushEvent := &events_gitlab.PushEvent{}
		if event.EventData.MessageIs(pushEvent) {
			err = event.EventData.UnmarshalTo(pushEvent)
			if err != nil {
				return false, err
			}
			lastCommit := ""
			if len(pushEvent.Commits) > 0 {
				lastCommit = pushEvent.Commits[len(pushEvent.Commits)-1].Title
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s 推送 %s(%s),包含%d次提交,最后提交内容 %s。", event.UserDisplayName, event.ProjectName,
				pushEvent.UserName, pushEvent.Project.Name, pushEvent.Project.Homepage, len(pushEvent.Commits), lastCommit)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Push {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		tagEvent := &events_gitlab.TagEvent{}
		if event.EventData.MessageIs(tagEvent) {
			err = event.EventData.UnmarshalTo(tagEvent)
			if err != nil {
				return false, err
			}
			opt := "新增"
			if len(tagEvent.Commits) == 0 {
				opt = "删除"
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s %s标签。", event.UserDisplayName, event.ProjectName,
				tagEvent.UserName, opt)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Tag {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		wikiEvent := &events_gitlab.WikiEvent{}
		if event.EventData.MessageIs(wikiEvent) {
			err = event.EventData.UnmarshalTo(wikiEvent)
			if err != nil {
				return false, err
			}
			opt := "创建"
			if wikiEvent.ObjectAttributes.Action == "update" {
				opt = "更新"
			} else if wikiEvent.ObjectAttributes.Action == "delete" {
				opt = "删除"
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s %s WIKI ", event.UserDisplayName, event.ProjectName,
				wikiEvent.User.Name, opt)
			if wikiEvent.ObjectAttributes.Action == "delete" {
				msg = msg + wikiEvent.ObjectAttributes.Title
			} else {
				msg = msg + fmt.Sprintf("%s(%s)", wikiEvent.ObjectAttributes.Title, wikiEvent.ObjectAttributes.Url)
			}
			msg = msg + "。"
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GitlabEvCfg.Wiki {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
